package com.heima.article.service.impl;


import com.alibaba.fastjson.JSON;
import com.heima.article.feign.FollowClient;
import com.heima.article.service.CollectionBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.Dtos.AllDto;
import com.heima.model.wemedia.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CollectionBehaviorServiceImpl implements CollectionBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private FollowClient followClient;

    @Autowired
    private KafkaTemplate kafkaTemplate;
    /**
     * 收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(AllDto dto) {
        if (dto.getEntryId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.setArticleId(dto.getEntryId());

        ApArticle apArticle = followClient.selectOne(dto);

        Integer userId = AppThreadLocalUtils.getUser().getId();

//        Map<String,Object> map = new HashMap<>();
//        map.put("id",dto.getEntryId());

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        mess.setArticleId(dto.getArticleId());

        if (dto.getOperation() == 0){

            //收藏
            cacheService.zAdd(BehaviorConstants.APUSER_BEHAVIOR_RELATION+userId+apArticle.getAuthorId(),"Collection",System.currentTimeMillis());
//            map.put("type",true);

            mess.setAdd(1);

        }
        else {
            //取消收藏
            cacheService.zRemove(BehaviorConstants.APUSER_BEHAVIOR_RELATION+userId+apArticle.getAuthorId(),"Collection");
//            map.put("type",false);
            mess.setAdd(-1);
        }

        //kafka数据口同步数据
//        kafkaTemplate.send(WmNewsMessageConstants.COLLECTION_BEHAVIOR, JSON.toJSONString(map));
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));

        return ResponseResult.okResult("ok");
    }

    /**
     * 数据回显
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(AllDto dto) {

        if (dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = followClient.selectOne(dto);

        Integer userId = AppThreadLocalUtils.getUser().getId();

        Boolean isfollow = false;
        Boolean islike = false;
        Boolean isunlike = false;
        Boolean iscollection = false;

        //是否关注
        Double follow = cacheService.zScore(BehaviorConstants.APUSER_FOLLOW_RELATION + userId + apArticle.getAuthorId(), "follow");
        if (follow != null){
            isfollow = true;
        }

        //是否点赞
        Double likes = cacheService.zScore(BehaviorConstants.APUSER_LIKES_RELATION + userId + apArticle.getAuthorId(), "Likes");
        if (likes != null){
            islike = true;
        }

        //是否不喜欢
        Double unLikes = cacheService.zScore(BehaviorConstants.APUSER_UNLIKES_RELATION + userId + apArticle.getAuthorId(), "UNLikes");
        if (unLikes != null){
            isunlike = true;
        }
        //是否收藏
        Double collection = cacheService.zScore(BehaviorConstants.APUSER_BEHAVIOR_RELATION + userId + apArticle.getAuthorId(), "Collection");
        if (collection != null){
            iscollection = true;
        }

        Map<String,Boolean> map = new HashMap<>();
        map.put("isfollow",isfollow);
        map.put("islike",islike);
        map.put("isunlike",isunlike);
        map.put("iscollection",iscollection);

        return ResponseResult.okResult(map);
    }
}
